// B3GDlg.cpp : implementation file
//

#include "stdafx.h"
#include "YL_DEMO_VOIP.h"
#include "B3GDlg.h"
#include "YL_DEMO_VOIPDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CB3GDlg dialog


CB3GDlg::CB3GDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CB3GDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CB3GDlg)
	m_strPSTNDest = _T("");
	m_strUSBDest = _T("");
	//}}AFX_DATA_INIT
	m_pParent = pParent;
}


void CB3GDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CB3GDlg)
	DDX_Text(pDX, IDC_PSTNDEST, m_strPSTNDest);
	DDX_Text(pDX, IDC_USBDEST, m_strUSBDest);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CB3GDlg, CDialog)
	//{{AFX_MSG_MAP(CB3GDlg)
	ON_BN_CLICKED(IDC_SETRING, OnSetring)
	ON_BN_CLICKED(IDC_SETTONE, OnSettone)
	ON_BN_CLICKED(IDC_DIVERTUSB, OnDivertusb)
	ON_BN_CLICKED(IDC_DIVERTPSTN, OnDivertpstn)
	ON_BN_CLICKED(IDC_CHANNEL, OnChannel)
	ON_BN_CLICKED(IDC_SETLED, OnSetled)
	ON_BN_CLICKED(IDC_TEST, OnTest)
	//}}AFX_MSG_MAP
	ON_STN_CLICKED(IDC_CHANNELSTATE, &CB3GDlg::OnStnClickedChannelstate)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CB3GDlg message handlers

void CB3GDlg::OnSetring() 
{
	static BOOL bRing = TRUE;

	YL_DeviceIoControl(YL_IOCTL_SET_B3GCURRENTCHANNEL, (LPVOID)CHANNEL_USB);
	
	YL_DeviceIoControl(YL_IOCTL_SET_RING, (LPVOID)bRing, 0);
	
	if ( bRing )
		GetDlgItem(IDC_RINGSTATE)->SetWindowText( _T("START_RING") );
	else
		GetDlgItem(IDC_RINGSTATE)->SetWindowText( _T("STOP_RING") );
	
	bRing = ! bRing;
}

void CB3GDlg::OnSettone() 
{
	static int eType = PHONE_TONE_SILENCE;
	
	YL_DeviceIoControl(YL_IOCTL_SET_TONE, (LPVOID)eType);
	
	switch ( eType )
	{
	case PHONE_TONE_UNKNOW:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_UNKNOW") );
		break;
	case PHONE_TONE_SILENCE:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_SILENCE") );
		break;
	case PHONE_TONE_MUTE:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_MUTE") );
		break;
	case PHONE_TONE_DIAL:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_DIAL") );
		break;
	case PHONE_TONE_CONNECTING:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_CONNECTING") );
		break;
	case PHONE_TONE_RINGBACK:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_RINGBACK") );
		break;
	case PHONE_TONE_BUSY:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_BUSY") );
		break;
	default:
		break;
	}
	
	eType++;
	
	if ( eType > PHONE_TONE_BUSY )
		eType = PHONE_TONE_SILENCE;
}

void CB3GDlg::OnChannel() 
{
	short sChannel = 0;
	YL_DeviceIoControl(YL_IOCTL_GET_B3GCURRENTCHANNEL, &sChannel);
#if 0
	
	YL_DeviceIoControl(YL_IOCTL_SET_B3GCURRENTCHANNEL, (LPVOID)CHANNEL_PSTN);
	YL_DeviceIoControl(YL_IOCTL_SET_B3GDEFAULTCHANNEL, (LPVOID)CHANNEL_PSTN);
#endif
	if ( CHANNEL_USB != sChannel )
	{
		YL_DeviceIoControl(YL_IOCTL_SET_B3GCURRENTCHANNEL, (LPVOID)CHANNEL_USB);
		YL_DeviceIoControl(YL_IOCTL_SET_B3GDEFAULTCHANNEL, (LPVOID)CHANNEL_USB);
		GetDlgItem(IDC_CHANNELSTATE)->SetWindowText( _T("CHANNEL_USB") );
	}
	else
	{
		YL_DeviceIoControl(YL_IOCTL_SET_B3GCURRENTCHANNEL, (LPVOID)CHANNEL_PSTN);
		YL_DeviceIoControl(YL_IOCTL_SET_B3GDEFAULTCHANNEL, (LPVOID)CHANNEL_PSTN);
		GetDlgItem(IDC_CHANNELSTATE)->SetWindowText( _T("CHANNEL_PSTN") );
	}
}


void CB3GDlg::OnSetled() 
{
	static int iState = 0;
	
	YL_DeviceIoControl(YL_IOCTL_SET_B3GLED, (LPVOID)iState);
	
	CString strNote = _T("");
	strNote.Format( _T("LED State %d"), iState );
	GetDlgItem(IDC_LEDSTATE)->SetWindowText( strNote );
	
	iState = (++iState)%4;
}


void CB3GDlg::OnDivertusb() 
{
	// USB CALL IN
	YL_DeviceIoControl(YL_IOCTL_SET_B3GCURRENTCHANNEL, (LPVOID)CHANNEL_USB);
	YL_DeviceIoControl(YL_IOCTL_SET_RING, (LPVOID)TRUE);

	// CONNECT TO PSTN
	YL_DeviceIoControl(YL_IOCTL_SET_B3GPSTNHOOK, (LPVOID)FALSE);
	YL_DeviceIoControl(YL_IOCTL_SET_B3GPLAYTOPSTN, (LPVOID)TRUE);
	YL_DeviceIoControl(YL_IOCTL_SET_B3GLED, (LPVOID)3);
	YL_DeviceIoControl(YL_IOCTL_SET_B3GTONEDETECT, (LPVOID)TRUE);

	// DIAL TO PSTN
	Sleep( 1000 );
	UpdateData( TRUE );
	YL_DeviceIoControl(YL_IOCTL_SET_B3GDIALNUMBER, (LPVOID)m_strPSTNDest[0]);
	Sleep( 300 );
	YL_DeviceIoControl(YL_IOCTL_SET_B3GDIALNUMBER, (LPVOID)m_strPSTNDest[1]);
	Sleep( 300 );
	YL_DeviceIoControl(YL_IOCTL_SET_B3GDIALNUMBER, (LPVOID)m_strPSTNDest[2]);
	// ....

	// STOP USB RING
	YL_DeviceIoControl(YL_IOCTL_SET_RING, FALSE);

	
/*
	// The other way to Divert usb call:
	UpdateData( TRUE );
	YL_DeviceIoControl(YL_IOCTL_DIVERT_USBCALL, m_strPSTNDest);
*/
}

void CB3GDlg::OnDivertpstn() 
{
	// PSTN CALL IN
	// ...

	// ANSWER PSTN CALL
	YL_DeviceIoControl(YL_IOCTL_SET_B3GPSTNHOOK, (LPVOID)FALSE);

	// CONNECT TO USB
	YL_DeviceIoControl(YL_IOCTL_SET_B3GPLAYTOPSTN, (LPVOID)TRUE);
	YL_DeviceIoControl(YL_IOCTL_SET_B3GLED, (LPVOID)3);
	YL_DeviceIoControl(YL_IOCTL_SET_B3GTONEDETECT, (LPVOID)TRUE);
	
	// MAKE USB CALL
	UpdateData( TRUE );
	// CALL m_strUSBDest	
	// ....
	YL_DeviceIoControl(YL_IOCTL_SET_TONE, (LPVOID)PHONE_TONE_CONNECTING);

/*
	// The other way to Divert pstn call:
	UpdateData( TRUE );
	YL_DeviceIoControl(YL_IOCTL_DIVERT_PSTNCALL, m_strPSTNDest);
*/
	
}


void CB3GDlg::OnTest() 
{
	// Start from everything is initialized
	//pParent->m_XUSB.AllPhCmdVoIPReady();
	// OFFHOOK the HandSet

	// Set current channel to USB
	YL_DeviceIoControl(YL_IOCTL_SET_B3GCURRENTCHANNEL, (LPVOID)CHANNEL_USB);

	Sleep( 1000 );

	YL_DeviceIoControl(YL_IOCTL_SET_B3GPLAYTOPSTN, (LPVOID)TRUE);

	YL_DeviceIoControl(YL_IOCTL_SET_B3GPSTNHOOK, (LPVOID)FALSE);
	
	Sleep( 1000 );
	
	// Dial PSTN number
	YL_DeviceIoControl(YL_IOCTL_SET_B3GDIALNUMBER, (LPVOID)'2');
	Sleep( 300 );
	YL_DeviceIoControl(YL_IOCTL_SET_B3GDIALNUMBER, (LPVOID)'0');
	Sleep( 300 );
	YL_DeviceIoControl(YL_IOCTL_SET_B3GDIALNUMBER, (LPVOID)'7');
	
}

void CB3GDlg::OnStnClickedChannelstate()
{
	// TODO: Add your control notification handler code here
}
